﻿using System;
using System.Linq;
using System.Text;
using Kaidou.Model;
using System.Data;
using System.Data.SqlClient;
using Kaidou.Util;
using System.Collections;
using Kaidou.Model;


namespace Kaidou.Persister
{
    public class CountSealByUnionPersister
    {

        /**
	     * 組合員別購入シール枚数を取得する。
	     * @param con コネクション
	     * @param purch_seal データモデル
	     * @return TRN_Purch_Seal データモデル
	     */
	    public void countSealByUnion(InputErrorException ie, SqlDB db, TrnPurchSeal_a model) {
            LogManager.info("CountSealByUnionPersister countSealByUnion in");

            ArrayList union_office_name = new ArrayList();
            ArrayList union_seal_qty = new ArrayList();
		    
		    int sumSealQty = 0;

		    String pssql =
			    "select U.office_name, count(S.seal_no) as cnt, U.union_code, U.branch_code "
                    + " from trn_purch_seal S inner join mst_union_view U on (U.branch_code = S.branch_code) and (S.union_code = U.union_code) "
                    + " where S.seal_no >= @seal_str_no and S.seal_no <= @seal_end_no "
				    + " group by U.office_name, U.union_code, U.branch_code";

		    try {
                SqlParameter[] sqlParameter = {   
                                                new SqlParameter("@seal_str_no",SqlDbType.VarChar),
                                                new SqlParameter("@seal_end_no",SqlDbType.VarChar)
                                               };
                sqlParameter[0].Value = model.SealStrNo;
                sqlParameter[1].Value = model.SealEndNo;
                SqlDataReader dataReader = db.GetDataCmd(pssql, sqlParameter);

                while (dataReader.Read())
                {
                    int branch_code = (int)dataReader["branch_code"];
                    LogManager.info("CountSealByUnionPersister countSealByUnion branch_code " + branch_code);
                    if (branch_code == 0)
                    {
                        string office_name = (string)dataReader["office_name"];
                        union_office_name.Add(office_name + "&nbsp;&nbsp;本店");
				    } else {
                        ArrayList vctBranchSearchModel = new ArrayList();
					    MST_UnionPersister MUper = new MST_UnionPersister();
					    vctBranchSearchModel = MUper.getByUnionCode(db,(string)dataReader["union_code"] );
					    BranchSearchModel branchSearchModel = new BranchSearchModel();
					    String strUnionBranchName = "";
                        strUnionBranchName = (string)dataReader["office_name"];
					    for (int ii = 0; ii < vctBranchSearchModel.Count; ii++) {
						    branchSearchModel = (BranchSearchModel) vctBranchSearchModel[ii];
						    if (branchSearchModel.Branch_code == 0) {
                                strUnionBranchName = branchSearchModel.Office_name + "&nbsp;&nbsp;" + strUnionBranchName;
						    }
						    branchSearchModel = null;
					    }
					    union_office_name.Add(strUnionBranchName);
					    vctBranchSearchModel = null;
					    MUper = null;
				    }
				    union_seal_qty.Add(((int)dataReader["cnt"]).ToString());
                    sumSealQty += (int)dataReader["cnt"];
                    LogManager.info("CountSealByUnionPersister countSealByUnion sumSealQty " + sumSealQty);
			    }

			    if ((int.Parse(model.SealEndNo) - int.Parse(model.SealStrNo) + 1) != sumSealQty) {
				    ie.addErrorCode(ErrorCodeDefine.SEAL_COUNT_IS_NOT_AGREE); // 組合員別のシール枚数の合計が入力枚数と一致しません。
			    }
			    model.setUnion_office_name(union_office_name);
			    model.setUnion_seal_qty(union_seal_qty);

                dataReader.Close();
		    } catch (Exception e) {
                LogManager.error(" CountSealByUnionPersister countSealByUnion "+e.Message+e.StackTrace);
		    } 
        }
             
    }
}
